﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Mail;
using System.Data.SqlServerCe;
using System.Data;
using Microsoft.Win32;
using System.IO;
/**
 * KHALFAOUI Mahdi - Eleria.fr
 * 
 * */
namespace EleriaMailing
{
    /// <summary>
    /// Logique d'interaction pour editeurContenu.xaml
    /// </summary>
    public partial class editeurContenu : UserControl
    {
        public editeurContenu()
        {
            InitializeComponent();
        }    
        private const string TITRE = "Edition";
        private string nomFichier = "Sans nom";
        private bool verifOrtho = false;

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            rtb_ContenuEmail.Document.Blocks.Clear();
            nomFichier = "Sans nom";            
        }
        private void btnSpellCheck_Click(object sender, RoutedEventArgs e)
        {
            verifOrtho = !verifOrtho;
            rtb_ContenuEmail.SpellCheck.IsEnabled = verifOrtho;
        }
        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            OpenFile();           
        }

        private void OpenFile()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Choisir fichier : ";
            ofd.InitialDirectory = "c:\\";
            ofd.Filter = "Rich Text Format (*.rtf)|*.rtf|Fichier Texte (*.txt)|*.txt|Tout fichier (*.*)|*.*";
            if (ofd.ShowDialog() == true)
            {
                nomFichier = ofd.FileName;
                LoadFile(ofd.FileName);
            }
        }

        private void LoadFile(string fileName)
        {
            TextRange range = new TextRange(rtb_ContenuEmail.Document.ContentStart, rtb_ContenuEmail.Document.ContentEnd);
            FileInfo fileInfo = new FileInfo(fileName);
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    if (fileInfo.Extension == ".rtf")
                        range.Load(fs, System.Windows.DataFormats.Rtf);
                    else
                        range.Load(fs, System.Windows.DataFormats.Text);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message, "Erreur fichier");
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            bool saveAs = false;
            if (sender is System.Windows.Controls.MenuItem)
            {
                saveAs = (((System.Windows.Controls.MenuItem)sender).Header.ToString() == "Sauver comme");
            }
            if (saveAs | nomFichier.Equals("Sans nom"))
            {
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.Title = "Sauver comme";
                saveDialog.Filter = "Rich Text Format (*.rtf)|*.rtf|Fichier texte(*.txt)|*.txt|Tout cichier (*.*)|*.*";
                saveDialog.InitialDirectory = "c:\\";

                if (saveDialog.ShowDialog() == true)
                {
                    SaveFile(saveDialog.FileName);                    
                }
            }
            else
                SaveFile(nomFichier);
        }

        private void SaveFile(string tempFileName)
        {
            TextRange range = new TextRange(rtb_ContenuEmail.Document.ContentStart, rtb_ContenuEmail.Document.ContentEnd);
            FileInfo newFile = new FileInfo(tempFileName);
            try
            {
                using (FileStream fs = new FileStream(tempFileName, FileMode.Create))
                {
                    if (newFile.Extension == ".rtf")
                        range.Save(fs, System.Windows.DataFormats.Rtf, true);
                    else
                        range.Save(fs, System.Windows.DataFormats.Text, false);
                }
                nomFichier = tempFileName;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message, "Erreur fichier");
            }
        }

        // Fonctions qui ajoutent des tags pour le publipostage
        private void btNom_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange(rtb_ContenuEmail.Selection.Start, rtb_ContenuEmail.Selection.End);
            tr.Text = "[NOM]";
        }
        private void btPrenom_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange(rtb_ContenuEmail.Selection.Start, rtb_ContenuEmail.Selection.End);
            tr.Text = "[PRENOM]";
        }
        private void btGenre_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange(rtb_ContenuEmail.Selection.Start, rtb_ContenuEmail.Selection.End);
            tr.Text = "[GENRE]";
        }
        private void btSociete_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange( rtb_ContenuEmail.Selection.Start, rtb_ContenuEmail.Selection.End);
            tr.Text = "[SOCIETE]";
        }
        private void btMail_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange( rtb_ContenuEmail.Selection.Start, rtb_ContenuEmail.Selection.End);
            tr.Text = "[EMAIL]";
        }
        private void btDate_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange( rtb_ContenuEmail.Selection.Start, rtb_ContenuEmail.Selection.End);
            tr.Text = DateTime.Now.ToShortDateString();
        }
        private void btFichierJoint_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".*";
            dlg.Filter = "Fichier |*.*";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                string filename = dlg.FileName;
                tb_fichierJoint.Text = filename;
            }
        }
    }
}
